Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.10% covered (success)
93.10%
27 / 29
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
InseuisAnalyser
93.10% covered (success)
93.10%
27 / 29
71.43% covered (warning)
71.43%
5 / 7
11.04
0.00% covered (danger)
0.00%
0 / 1
 analyse
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 checkTransaction
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
1.00
 checkSpan
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
1.01
 checkHttpSpan
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRequestTransactionSuccessful
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 isJobTransactionSuccessful
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isCommandTransactionSuccessful
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Inseuis\Services;
4
5use Illuminate\Support\Collection;
6use Inseuis\Contracts\InseuisAnalyserContract;
7use Inseuis\Struct\Spans\AbstractSpan;
8use Inseuis\Struct\Spans\HttpSpan;
9use Inseuis\Struct\Spans\QuerySpan;
10use Inseuis\Struct\Spans\RedisCommandSpan;
11use Inseuis\Struct\Transactions\AbstractTransaction;
12use Inseuis\Struct\Transactions\CommandTransaction;
13use Inseuis\Struct\Transactions\JobTransaction;
14use Inseuis\Struct\Transactions\RequestTransaction;
15
16class InseuisAnalyser implements InseuisAnalyserContract
17{
18    /**
19     * @param Collection<array-key, AbstractSpan> $spans
20     */
21    public function analyse(AbstractTransaction $transaction, Collection $spans, ?int $allowedExitCode): void
22    {
23        $this->checkTransaction($transaction, $allowedExitCode);
24        foreach ($spans as $span) {
25            $this->checkSpan($span);
26        }
27    }
28
29    protected function checkTransaction(AbstractTransaction $transaction, ?int $allowedExitCode): void
30    {
31        $transaction->successful = match (true) {
32            $transaction instanceof RequestTransaction => $this->isRequestTransactionSuccessful(
33                $transaction,
34                $allowedExitCode
35            ),
36            $transaction instanceof JobTransaction     => $this->isJobTransactionSuccessful($transaction),
37            $transaction instanceof CommandTransaction => $this->isCommandTransactionSuccessful(
38                $transaction,
39                $allowedExitCode
40            ),
41            default => null
42        };
43    }
44
45    protected function checkSpan(AbstractSpan $span): void
46    {
47        $span->successful = match (true) {
48            $span instanceof HttpSpan => $this->checkHttpSpan($span),
49            $span instanceof QuerySpan, $span instanceof RedisCommandSpan => true,
50            default => null
51        };
52    }
53
54    protected function checkHttpSpan(HttpSpan $span): bool
55    {
56        return $span->responseCode < 400;
57    }
58
59    protected function isRequestTransactionSuccessful(RequestTransaction $transaction, ?int $allowedExitCode): bool
60    {
61        foreach ($transaction->getErrors() as $error) {
62            if (!$error->handled) {
63                return $transaction->responseCode < 500;
64            }
65        }
66
67        if ($allowedExitCode !== null) {
68            return $transaction->responseCode === $allowedExitCode;
69        }
70
71        return $transaction->responseCode < 500;
72    }
73
74    protected function isJobTransactionSuccessful(JobTransaction $transaction): bool
75    {
76        return !$transaction->failed;
77    }
78
79    protected function isCommandTransactionSuccessful(CommandTransaction $transaction, ?int $allowedExitCode): bool
80    {
81        return $transaction->exitCode === ($allowedExitCode ?? 0);
82    }
83}